package com.market.quote.modules.stock.infrastructure.db.repository;

import com.market.quote.modules.stock.domain.dto.StockInfoDTO;
import com.market.quote.modules.stock.domain.dto.StockRequestDTO;
import com.market.quote.modules.stock.domain.repository.StockRepository;
import com.market.quote.modules.stock.infrastructure.db.converter.StockConverter;
import com.market.quote.modules.stock.infrastructure.db.dao.StockDao;
import com.market.quote.modules.stock.infrastructure.db.model.Stock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author duweibin
 * @date 2024/4/22
 * @description 股票信息数据库操作实现
 */
@Repository
public class StockRepositoryImpl implements StockRepository {

    private final StockDao stockDao;
    private final StockConverter stockConverter;

    @Autowired
    public StockRepositoryImpl(StockDao stockDao, StockConverter stockConverter) {
        this.stockDao = stockDao;
        this.stockConverter = stockConverter;
    }


    @Override
    public StockInfoDTO getStockInfoById(Long id) {
        Stock stock = stockDao.queryById(id);
        return stockConverter.toStockInfoDTO(stock);
    }

    @Override
    public StockInfoDTO getStockInfoByDate(StockRequestDTO stockRequestDTO) {
        Stock stock = stockDao.queryByCode(stockRequestDTO);
        return stockConverter.toStockInfoDTO(stock);
    }

    @Override
    public List<StockInfoDTO> getStockInfoList(StockRequestDTO stockRequestDTO) {
        PageRequest pageable = PageRequest.of(stockRequestDTO.getPageNum(), stockRequestDTO.getPageSize());
        List<Stock> stockList = stockDao.queryAllByLimit(stockRequestDTO, pageable);
        return stockConverter.toStockInfoDTO(stockList);
    }

    @Override
    public void insertStock(StockInfoDTO stockInfoDTO) {
        Stock stock = stockConverter.toStock(stockInfoDTO);
        stockDao.insert(stock);
    }

    @Override
    public void batchInsertStock(List<StockInfoDTO> stockInfoDTOList) {
        List<Stock> stockList = stockConverter.toStock(stockInfoDTOList);
        stockDao.insertBatch(stockList);
    }

    @Override
    public void updateStock(StockInfoDTO stockInfoDTO) {
        Stock stock = stockConverter.toStock(stockInfoDTO);
        stockDao.update(stock);
    }

    @Override
    public void deleteStock(Long id) {
        stockDao.deleteById(id);
    }
}
